package services.user;

import models.pubobj.*;
import models.user.*;

import org.apache.commons.lang3.StringUtils;

import dto.user.*;

import play.Logger;
import play.mvc.Http;
import services.exception.*;
import services.security.*;
import utils.AniPatternUtils;
import utils.MVCUtils;

public class AniLocalUserService {

	//get user info
	public static AniUser currentLoginUser(){
        try{
            String userEmail = MVCUtils.pullLocalUserKeyFromCookie();
            return  findUserWithEmail(userEmail.trim());
        } catch (Exception e){
            throw new AniDataException("HAVE_NOT_LOGIN");
        }
	}
    public static AniUser getCurrentOperatesUser(){
        String userKey = null;
        try{
            userKey = MVCUtils.pullLocalUserKeyFromCookie();
        }catch(Exception e){
            userKey = MVCUtils.getRequestArgs(Http.Context.current().request()).get(MVCUtils.USER_KEY);
        }finally{
            return StringUtils.isEmpty(userKey) ? null : findUserWithEmail(userKey);
        }
    }
    public static AniUser findUserWithEmail(String userEmail){
        try{
            AniUser oneUser = AniUser.findByEmail(userEmail);
            if(oneUser == null){
                throw new AniAuthException();
            }
            return oneUser;
        } catch (Exception e){
            throw new AniAuthException("CANNOT_FIND_USER");
        }
    }
	//check user register info legality
	protected static void checkUserRegInfo(LocalUserInfoDto userRegInfo) {
		//need catch in controller
		checkRequiredUserRegisterInfo(userRegInfo);
		
		AniPatternUtils.checkEmailFormatLegality(userRegInfo.email);
		checkScreenNameLegality(userRegInfo.screenName);
		checkMd5PwdLegality(userRegInfo.md5Pwd);
		
		checkUserEmailUnique(userRegInfo.email);
		checkUserScreenNameUnique(userRegInfo.screenName);
	}
	protected static void checkUserUpdateInfo(LocalUserInfoDto userUpdateInfo){
		if(!StringUtils.isEmpty(userUpdateInfo.screenName)){
			checkScreenNameLegality(userUpdateInfo.screenName);
			checkUserScreenNameUnique(userUpdateInfo.screenName);
		}
		if(!StringUtils.isEmpty(userUpdateInfo.md5Pwd)){
			checkMd5PwdLegality(userUpdateInfo.md5Pwd);
		}
	}
	private static void checkRequiredUserRegisterInfo(LocalUserInfoDto localUserRegInfo){
		if(StringUtils.isEmpty(localUserRegInfo.email)){
			throw new AniAuthException("EMAIL_IS_REQUIRED");
		}
		if(StringUtils.isEmpty(localUserRegInfo.md5Pwd)){
			throw new AniAuthException("PWD_IS_REQUIRED");
		}
	}
	//check user password md5
	protected static void checkMd5PwdLegality(String md5Pwd){
		try{
			AniPatternUtils.checkMD5FormatLegality(md5Pwd);
		}catch(Exception e){
			throw new AniAuthException("MD5_PWD_ILLEGAL");
		}
	}
	//check user unique
	protected static void checkUserEmailUnique(String userEmail){
		AniUser curUser = AniUser.findByEmail(userEmail.trim());
		if(curUser != null){
			throw new AniAuthException("USER_EMAIL_EXISTS");
		}
	}
	//---check user screenname unique
	protected static void checkUserScreenNameUnique(String screenName){
		AniUser curUser = AniUser.findByScreenName(screenName);
		if(curUser != null){
			throw new AniAuthException("USER_SCREENNAME_EXISTS");
		}
	}
	//-save user
    protected static void saveUser(LocalUserInfoDto oneLocalUserRegisterInfo, AniUserRole role){
        new AniUser(
                oneLocalUserRegisterInfo.email,
                oneLocalUserRegisterInfo.md5Pwd,
                AniSecurityService.getObjectStateWithKey(AniObjectState.States.ACTIVATED.name()),
                role,
                oneLocalUserRegisterInfo.screenName
        );
    }
	//--user role existence
	protected static AniUserRole getUserRoleWithKey(String roleKey){
		AniUserRole oneUserRole = AniUserRole.findByRoleKey(roleKey);
		if(oneUserRole == null){
			throw new AniRuleException("USER_ROLE_FORMAT_ILLEGAL");
		}
		return oneUserRole;
	}
	
	//screenname legality
	public static void checkScreenNameLegality(String screenName){
		if(!AniPatternUtils.isMatches(screenName.trim(), AniPatternUtils.SCREENNAME_PATTERN)){
			throw new AniRuleException("SCREENNAME_FORMAT_ILLEGAL");
		}
	}
}
